#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int k;
		cin>>k;
		unordered_map<int, int> a;
		unordered_map<int, int> b;
		int n, x;
		cin>>n;
		for(int i = 1;i <= n;i++)
		{
			cin>>x;
			a[x]++;
		}
		cin>>n;
		for(int i = 1;i <= n;i++)
		{
			cin>>x;
			b[x]++;
		}
		int cnt = 0;
		for(auto i = a.begin();i != a.end();i++)
		{
			int target = k-i->first;
			auto idx = b.find(target);
			if(idx != b.end())
				cnt += i->second*idx->second;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

